﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel;
using B10_Ex02_Elad_034695114_Meyron_026607788;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using B10_Ex02_EladLebovitch_034695114.Interfaces;
using Infrastructure.Managers;

namespace B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel
{
    public class EnemyBullet : Sprite, IEnemyObjects , ICollidable2D, IHarmful
    {
        private const int k_BulletSpeed = 190;
        private const string k_AssetName = @"Sprites\Bullet";
      
        public EnemyBullet(Game i_game, Vector2 i_StartPos)
            : base(k_AssetName, i_game)
        {
            // Set the starting position
            this.PositionOfOrigin = i_StartPos;

            // Player's bullet is red
            this.TintColor = Color.Blue;

            // Set the speed
            this.Velocity = new Vector2(0, k_BulletSpeed);
            i_game.Components.Add(this);
            ((ICollisionsManager)i_game.Services.GetService(typeof(ICollisionsManager))).AddObjectToMonitor(this);
        }

        public override void Update(GameTime gameTime)
        {
            // Check if the bullet went out of bounds
            if (this.PositionOfOrigin.Y > this.Game.GraphicsDevice.Viewport.Height)
            {
                Distroy();
            }

            base.Update(gameTime);
        }

        public override void Collided(ICollidable i_Collidable)
        {
            // If it's an enemy bullet
            if (i_Collidable is IPlayerObjects && (i_Collidable is IHarmful))
            {
                const int v_DestroyFreq = 2;

                // Check if the bullets should be destroyed
                if (((UtilityService)(Game.Services.GetService(typeof(UtilityService)))).RandomCondition(v_DestroyFreq))
                {
                    this.Distroy();
                }
            }
            // If it's an enemy sprite
            else if (i_Collidable is IPlayerObjects)
            {
                this.Distroy();
            }
        }
    }
}
